package com.myproject.filmrecommendationsys.controller;


import com.myproject.filmrecommendationsys.util.AjaxResult;
import com.myproject.filmrecommendationsys.util.LocalUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.myproject.filmrecommendationsys.domain.Comment;
import com.myproject.filmrecommendationsys.service.ICommentService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 评论Controller
 */
@RestController
@RequestMapping("/comment")
@Api(tags="评论管理")
public class CommentController
{
    @Autowired
    private ICommentService commentService;
    @Autowired
    private LocalUploadUtil localUploadUtil;

    /**
     * 查询评论列表
     */
    @GetMapping("/list")
    @ApiOperation("查询评论列表")
    public AjaxResult list(Comment comment)
    {
        return AjaxResult.success(commentService.selectCommentList(comment));
    }


    /**
     * 获取评论详细信息
     */

    @GetMapping(value = "/{id}")
    @ApiOperation("获取评论详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(commentService.selectCommentById(id));
    }

    /**
     * 新增评论
     */


    @PostMapping("/add")
    @ApiOperation("新增评论")
    public AjaxResult add(@RequestBody Comment comment ,@RequestPart("file") MultipartFile file)
    {
        if(file.isEmpty()){
            return AjaxResult.error(400,"上传的文件为空");
        }
        String arr[] = file.getOriginalFilename().split("\\.");
        System.out.println("获取到的文件后缀为"+arr[arr.length-1]);
        localUploadUtil.upload(file);
        System.out.println("上传成功");
        return AjaxResult.success(commentService.insertComment(comment));
    }

    /**
     * 修改评论
     */

    @PutMapping("/update")
    @ApiOperation("修改评论")
    public AjaxResult edit(@RequestBody Comment comment)
    {
        return AjaxResult.success(commentService.updateComment(comment));
    }

    /**
     * 删除评论
     */

	@DeleteMapping("/{ids}")
    @ApiOperation("删除评论")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return AjaxResult.success(commentService.deleteCommentByIds(ids));
    }
}
